import Vue from 'vue';
import Vuex from 'vuex';

Vue.use(Vuex);

const state = {
    count: 12,
    info: 'this is store'
}

//同步
const mutations = {
    add(state, n) {
        state.count += n;
    },
    reduce(state) {
        state.count--;
    }
}

//异步
const actions = {
    //context 上下文对象， store 本身
    addAction(context) {
        context.commit('add', 10);
        //异步调用reduce
        setTimeout(() => {
            context.commit('reduce')
        }, 3000);
        console.log('我比reduce 先执行')
    },
    reduceAction({
        commit
    }) {
        commit('reduce');
    }
}

const getters = {
    count: function(state) {
        return state.count += 10;
    }
}

const moduleA = {
    state,
    mutations,
    getters,
    actions
}

export default new Vuex.Store({
    modules: { a: moduleA }
})